package com.itic.market.system.tixian.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.market.system.tixian.persistence.model.Tixian;
import com.itic.market.system.tixian.persistence.model.TixianExample;
import com.itic.market.system.tixian.service.TixianService;


@Controller
@RequestMapping(value="/tixian")
public class TixianController
     extends BaseController {
    @Autowired
    private TixianService tixianService;

    @Autowired
   	protected OutCommonService outCommonService;
       
       //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
       private static final String MODEL_NAME_CHINA = "提现管理";//中文名
       private static final String MODEL_NAME＿ENGLISH = "tixian";//英文名
       private static final String PAGE_COMMON_PATH = "/mall/system/tixian/tixian";//通用路径
       private static final String COMMON_URL = "/system/tixian/";//通用路径


       //////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
      
       /*
        * 主页面
        */
       @RequestMapping(value="/init")
       public String init(Model model){
	       	model.addAttribute("name_china", MODEL_NAME_CHINA);
	       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
	       	model.addAttribute("common_url", COMMON_URL);
	       	return PAGE_COMMON_PATH+"Main";
       }
       
       
       /*
        * 查看页面
        */
       @RequestMapping(value="/initView")
       public String initView(String id , Model model){
       	    if( StringHelper.isNotBlank(id) ) {
	       		
	       		Tixian temp  = tixianService.get(id);
	       		model.addAttribute("viewEdit",iticJsonMapper.toJson(temp));
	       		model.addAttribute("name_china", MODEL_NAME_CHINA);
	           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
	           	model.addAttribute("common_url", COMMON_URL);
        	}else {
                throw new ParameterErrorException();
            }
       	
         	return PAGE_COMMON_PATH+"View";
       }
       
       
       /*
        * 查看页面
        */
       @RequestMapping(value="/initFafang")
       public String initFafang(String id , Model model){
       	    if( StringHelper.isNotBlank(id) ) {
	       		
	       		Tixian temp  = tixianService.get(id);
	       		model.addAttribute("viewEdit",iticJsonMapper.toJson(temp));
	       		model.addAttribute("name_china", MODEL_NAME_CHINA);
	           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
	           	model.addAttribute("common_url", COMMON_URL);
        	}else {
                throw new ParameterErrorException();
            }
       	
         	return PAGE_COMMON_PATH+"Fafang";
       }
       
       
       //////////////////////////////////////////////////////业务代码区////////////////////////////////////////////////////////////
       
       
       
       /**
        * <p>Title:queryCondition </p>
        * <p>Description:封装查询条件 </p>
        * @param jzdw
        * @param jzdwExample
        * @return
        */
       private TixianExample queryCondition(HttpServletRequest request,Tixian temp) {
       	
       	// 参数处理
           TixianExample example = new TixianExample();
           // 按创建时间倒序
           example.setOrderByClause("create_time desc");
           // 按排列号顺序
           example.setOrderByClause("reorder asc");
           
           example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE );
         
           // 查询条件
           if ( null != temp ) {
        	   
           }
           return example;
       }
       
       /**
        * 
        * @param goldType
        * @param request
        * @return
        */
       @RequestMapping(value = "/list")
       @ResponseBody
       public Map<String, Object> list(Tixian temp, HttpServletRequest request){
           // 声明返回对象
           Map<String, Object> resultMap = new HashMap<String, Object>();
           
           
           TixianExample example = this.queryCondition(request,temp);
           // 封装分页参数
           example = this.handlePageQuery(request, example);
           
           
           // 获取信息总条数
           int total = tixianService.count(example);
           
           // 获取数据
           List<Tixian> list = tixianService.list(example);
           
           
           // 数据返回处理
           resultMap.put(IticConst.TOTAL_RECORDS, total);
           resultMap.put(IticConst.DISPLAY_RECORDS, total);
           resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
           resultMap.put(IticConst.DATA, list);
           return resultMap;

       }
       
       

       /**
        * 删除
        * 
        */
       @RequestMapping(value = "/fafang")
       @ResponseBody
       public Map<String, String> fafang(String id) {
           Map<String, String> resultMap = new HashMap<String, String>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           Tixian temp = tixianService.get(id);
           temp.setIsGive("1");
           temp.setUpdateTime(new Date());
           
           result = tixianService.edit(temp);
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       }
       
       /**
        * 删除
        * 
        */
       @RequestMapping(value = "/falsess")
       @ResponseBody
       public Map<String, String> falsess(String id) {
           Map<String, String> resultMap = new HashMap<String, String>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           Tixian temp = tixianService.get(id);
           temp.setIsGive("2");
           temp.setUpdateTime(new Date());
           temp.setNote("支付宝账号名与支付宝账号不符，请重新提交！");
           result = tixianService.falseee(temp);
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       }
}